<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 表格区域 -->
    <el-table :data="userList" border style="width: 100%" class="tab">
      <el-table-column prop="uid" label="用户ID"> </el-table-column>
      <el-table-column prop="account" label="用户名"> </el-table-column>
      <el-table-column prop="phone" label="预留电话"> </el-table-column>
      <el-table-column prop="create_time" label="账号注册时间">
        <template slot-scope="scope">{{ $toDate(scope.row.create_time) }}</template>
      </el-table-column>
      <el-table-column prop="operation" label="操作">
        <template slot-scope="scope">
          <!-- 修改按钮 -->
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editUserData(scope.row.uid)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 用户信息修改框 -->
    <!-- 修改对话框 -->
    <el-dialog title="信息更改" :visible.sync="updateVisible">
      <el-form :model="editForm">
        <el-form-item label="用户名：" :label-width="formLabelWidth">
          <el-input v-model="editForm.account" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电 话：" :label-width="formLabelWidth">
          <el-input v-model="editForm.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateInfo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, updateUserList } from '@/api/usermanage'
export default {
  name: 'UserManage',
  data() {
    return {
      userList: [],
      editForm: {
        account: '',
        phone: '',
        uid: 0,
      },
      queryInfo: {
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      updateVisible: false,
      formLabelWidth: '80px',
    }
  },
  created() {
    this.getUserInfoList()
  },
  methods: {
    async getUserInfoList() {
      const result = await getUserList({ pagenum: this.queryInfo.pagenum })
      if (result.status != 0) return this.$message.error(result.message)
      this.userList = result.data
      this.total = result.total.total
    },
    editUserData(uid) {
      this.editForm.uid = uid
      this.updateVisible = true
    },
    async updateInfo() {
      const result = await updateUserList({ ...this.editForm })
      if (result.status != 0) return this.$message.error(result.message)
      this.$message.success(result.message)
      this.updateVisible = false
      this.editForm.account = ''
      this.editForm.phone = ''
      this.editForm.uid = 0
      this.getUserInfoList()
    },
    // 分页区的当前页数据显示的条数
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getUserInfoList()
    },
    // 当前页面值
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getUserInfoList()
    },
  },
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 40px;
}
</style>
